package com.acdc.wechat.config;

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@Configuration
public class WechatPayConfig {

    @Value("${wechat.pay.merchant-id}")
    private String merchantId;
    
    @Value("${wechat.pay.merchant-serial-no}")
    private String merchantSerialNo;
    
    @Value("${wechat.pay.private-key-path}")
    private String privateKeyPath;
    
    @Value("${wechat.pay.api-v3-key}")
    private String apiV3Key;
    
    @Value("${wechat.pay.appid}")
    private String appid;
    
    @Value("${wechat.pay.cert-path}")
    private String certPath;

    @Bean
    public CloseableHttpClient wechatPayClient() throws FileNotFoundException, IOException, GeneralSecurityException, HttpCodeException, NotFoundException {
        
        // 加载商户私钥
        PrivateKey merchantPrivateKey;
        if (privateKeyPath.startsWith("classpath:")) {
            // 处理 classpath 资源
            String classPath = privateKeyPath.substring("classpath:".length());
            Resource resource = new ClassPathResource(classPath);
            try (InputStream inputStream = resource.getInputStream()) {
                merchantPrivateKey = PemUtil.loadPrivateKey(inputStream);
            }
        } else {
            // 处理文件路径
            merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(privateKeyPath));
        }
        
        // 加载平台证书
        CertificatesManager certificatesManager = CertificatesManager.getInstance();
        certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNo, merchantPrivateKey)), apiV3Key.getBytes());
        
        // 构建httpclient
        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(merchantId, merchantSerialNo, merchantPrivateKey)
                .withValidator(new WechatPay2Validator(certificatesManager.getVerifier(merchantId)))
                .build();
        
        return httpClient;
    }

    public String getMerchantId() {
        return merchantId;
    }

    public String getAppid() {
        return appid;
    }
    
    public String getApiV3Key() {
        return apiV3Key;
    }
}

